package priv.wangy.micro.common.utils.file;

import java.util.Arrays;
import java.util.List;

/**
 * 文件类型
 *
 * @author 张嘉琪
 * @since 2023-05-30
 */
public enum FileType {
    /**
     * PDF (Adobe Acrobat)
     */
    PDF("255044462D312E", "pdf"),
    /**
     * OFFICE Before 2003 doc or xls
     */
    OFFICE_2003("D0CF11E0", "doc", "xls"),
    /**
     * OFFICE After 2007 docx or xlsx
     */
    OFFICE_2007("504B0304", "docx", "xlsx"),
    /**
     * Bitmap format
     */
    BMP("424D", "bmp"),
    /**
     * GIF format
     */
    GIF("47494638", "gif"),
    /**
     * JPEG File Interchange Format
     */
    JPG("FFD8FFE0", "jpg"),
    /**
     * PNG format
     */
    PNG("89504E47", "png"),
    /**
     * TIFF format (Intel – little endian)
     */
    TIF("49492A00", "tif"),
    /**
     * ZIP Archive
     */
    ZIP("504B0304", "zip"),
    /**
     * RAR Archive
     */
    RAR("52617221", "rar");

    /**
     * 魔数
     */
    private final String magicNumber;

    /**
     * 后缀名
     */
    private final List<String> extension;

    FileType(String magicNumber, String... extensions) {
        this.magicNumber = magicNumber;
        this.extension = Arrays.asList(extensions);
    }

    public String getExtension() {
        return this.extension.get(0);
    }

    public List<String> getExtensions() {
        return this.extension;
    }

    public String getMagicNumber() {
        return this.magicNumber;
    }

    public static FileType[] getImgFileTypes() {
        return new FileType[]{BMP, GIF, JPG, PNG, TIF};
    }

    public static FileType[] getOfficeFileTypes() {
        return new FileType[]{OFFICE_2003, OFFICE_2007};
    }

    public static FileType[] getZipFileTypes() {
        return new FileType[]{ZIP, RAR};
    }

    public static FileType getByExtension(String extension) {
        for (FileType type : values()) {
            if (type.extension.contains(extension)) {
                return type;
            }
        }
        return null;
    }

    public static FileType getImgByExtension(String extension) {
        for (FileType type : getImgFileTypes()) {
            if (type.extension.contains(extension)) {
                return type;
            }
        }
        return null;
    }

    public static FileType getPdfByExtension(String extension) {
        if (PDF.extension.contains(extension)) {
            return PDF;
        }
        return null;
    }

    public static FileType getOfficeByExtension(String extension) {
        for (FileType type : getOfficeFileTypes()) {
            if (type.getExtensions().contains(extension)) {
                return type;
            }
        }
        return null;
    }

    public static FileType getZipByExtension(String extension) {
        for (FileType type : getZipFileTypes()) {
            if (type.getExtensions().contains(extension)) {
                return type;
            }
        }
        return null;
    }

    public static FileType getByMagicNumber(String magicNumber) {
        for (FileType type : values()) {
            if (type.magicNumber.equalsIgnoreCase(magicNumber)) {
                return type;
            }
        }
        return null;
    }
}
